/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sbmart.smartserver.job;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.LoggerFactory;
import sbmart.object.Header;
import sbmart.object.MstConnection;
import sbmart.smartserver.cache.DataSourcePool;
import sbmart.smartserver.task.ServerTrsTask;
import sbmart.util.LoggerYM;

/**
 *
 * @author albert
 *
 */
public class ServerTrsJob {

    private ExecutorService exec;
    private Hashtable<Integer, List<Header>> headerPool = new Hashtable<Integer, List<Header>>();
    private static boolean isGet = true;
    public static final org.slf4j.Logger LOG = LoggerFactory.getLogger("ServerTrs");
    //public static final LoggerYM YM = LoggerYM.getLogger();
    public static int count = 0;

    public void init() {

        DataSourcePool.instance();

        HashMap<Integer, MstConnection> branchs = DataSourcePool.instance().getMstConnection();
        List<Header> headers = new ArrayList<Header>();

        for (Iterator<MstConnection> it = branchs.values().iterator(); it.hasNext();) {
            MstConnection branch = it.next();
            Header header = new Header();
            header.setBranchId(branch.getBranchId());
            header.setIpaddress(branch.getIpaddress());
            header.setBranchName(branch.getBranchName());
            header.setNickname(branch.getNickname());
            header.setUsername(branch.getUsername());
            header.setPassword(branch.getPassword());
            headers.add(header);
        }
        headerPool.put(Integer.valueOf(0), headers);
    }

    public void runTasks() {
        LOG.debug("[RUN SMART SERVER]: "+count);
        try {
            if (exec == null || exec.isTerminated()) {
                count = 0;
                //YM.send("ServerTrs [RUN] TRUE : " + count);
                LOG.info("");
                LOG.info("");
                LOG.info("[TASK RUN] " + Calendar.getInstance().getTime().toString());
                executeGetTask();
            } else {
                count++;
                if (count > 1) {
                    //YM.sendBuzz();
                    //YM.send("ServerTrs [RUN] in progress : " + count);
                    LOG.info("ServerTrs [RUN] in progress : " + count);
                }

                if (count > 3) {
                    //YM.sendBuzz();
                    //YM.send("ServerTrs progress timeout, try to auto restart");
                    LOG.info("ServerTrs progress timeout, try to auto restart");
                        exec.shutdownNow();
                        Thread.currentThread().interrupt();
			exec = null;
                }
            }
        } catch (Exception ex) {
            LOG.error("[ERROR] runTask", ex);
        } finally {
        }
    }

    private void executeGetTask() {
        headerPool.clear();
        init();
        exec = Executors.newSingleThreadExecutor();
        for (int i = 0; i < headerPool.size(); i++) {
            exec.execute(new ServerTrsTask(headerPool.get(i), exec));
        }
        exec.shutdown();
    }
}
